package com.ztn.sdk.iot.api;

import com.fasterxml.jackson.core.type.TypeReference;
import com.ztn.sdk.iot.api.util.HttpTool;
import com.ztn.sdk.iot.api.req.DeviceBatchStatusReq;
import com.ztn.sdk.iot.api.req.DeviceGetReq;
import com.ztn.sdk.iot.api.req.DeviceListReq;
import com.ztn.sdk.iot.api.res.BaseRes;
import com.ztn.sdk.iot.api.res.Device;
import com.ztn.sdk.iot.api.res.DeviceStatus;
import com.ztn.sdk.iot.api.util.JsonTool;

import java.util.ArrayList;
import java.util.List;

public class IotDeviceTool {

    private static final String BASE_PATH = "iot/api/v1/v1/device/";

    public static List<DeviceStatus> batchDeviceStatus(DeviceBatchStatusReq req) {
        String path = BASE_PATH + "batchDeviceStatus";
        BaseRes<List<DeviceStatus>> res = HttpTool.doPost(path, JsonTool.toJson(req), new TypeReference<BaseRes<List<DeviceStatus>>>() {
        });
        if (null != res && null != res.data) {
            return res.data;
        }
        return new ArrayList<>(0);
    }

    public static List<Device> list(DeviceListReq req) {
        String path = BASE_PATH + "list";
        BaseRes<List<Device>> res = HttpTool.doPost(path, JsonTool.toJson(req), new TypeReference<BaseRes<List<Device>>>() {
        });
        if (null != res && null != res.data) {
            return res.data;
        }
        return new ArrayList<>(0);
    }

    public static Device get(DeviceGetReq req) {
        String path = BASE_PATH + "get";
        BaseRes<Device> res = HttpTool.doPost(path, JsonTool.toJson(req), new TypeReference<BaseRes<Device>>() {
        });
        if (null != res && null != res.data) {
            return res.data;
        }
        return null;
    }
}
